//
//  HMLoginView.swift
//  sina
//
//  Created by Kent on 16/9/25.
//  Copyright © 2016年 Ming. All rights reserved.
//

import UIKit

@objc protocol HMLoginViewDelegate: NSObjectProtocol {
    func loginClick ()
    @objc func registerClick ()
}

class HMLoginView: UIView {
    
    weak var delegate: HMLoginViewDelegate?
    
    

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        UISetup()
        
    }
    
    func updateInfo (content: String, imageName: String? = nil) {
        
        contentLabel.text = content
        
        if imageName != nil {
            
            circleView.image = UIImage(named: imageName!)
            middleIconView.isHidden = true
            coverView.isHidden = true
            
        } else {
            
            startAnimation()
            
        }
        
    }
    
    func startAnimation() {
        
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = M_PI * 2
        animation.repeatCount = MAXFLOAT
        animation.duration = 15
        animation.isRemovedOnCompletion = false
        circleView.layer.add(animation, forKey: nil)
        
    }
    
    func UISetup() {
        
        let c: CGFloat = 237/255.0
        backgroundColor = UIColor.init(red: c, green: c, blue: c, alpha: 1)
        
        let margin: CGFloat = 10.0
        
        addSubview(circleView)
        
        circleView.snp.makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-60)
        }
        
        addSubview(coverView)
        
        coverView.snp.makeConstraints { (make) in
            make.center.equalTo(circleView)
        }
        
        addSubview(middleIconView)
        
        middleIconView.snp.makeConstraints { (make) in
            make.center.equalTo(circleView)
        }
     
        addSubview(contentLabel)
        
        contentLabel.snp.makeConstraints { (make) in
            make.centerX.equalTo(circleView)
            make.top.equalTo(circleView.snp.bottom).offset(margin)
        }
        
        addSubview(loginBtn)
        
        loginBtn.snp.makeConstraints { (make) in
            make.left.equalTo(circleView.snp.left)
            make.top.equalTo(contentLabel.snp.bottom).offset(margin)
            make.width.equalTo(80)
        }
        
        addSubview(registerBtn)
        
        registerBtn.snp.makeConstraints { (make) in
            make.right.equalTo(circleView.snp.right)
            make.centerY.width.equalTo(loginBtn)
        }
        
    }
    
    //懒加载子视图
    lazy var circleView: UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_smallicon"))
    
    lazy var middleIconView: UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_house"))
    
    lazy var coverView: UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_mask_smallicon"))
    
    lazy var contentLabel: UILabel = {
        
        let label = UILabel(title: "haha", fontSize: 14, textColor: UIColor.darkGray)

        return label
        
    }()
    
    lazy var loginBtn: UIButton = {
        
        let lBtn = UIButton(title: "Login", fontSize: 14, textColor: UIColor.darkGray)
        lBtn.setBackgroundImage(#imageLiteral(resourceName: "common_button_white"), for: .normal)
        lBtn.addTarget(self, action: #selector(loginBtnClick), for: .touchUpInside)
        return lBtn
    }()
    
    func loginBtnClick() {
        delegate?.loginClick()
    }
    
    lazy var registerBtn: UIButton = {
        
        let rBtn = UIButton(title: "Register", fontSize: 14, textColor: UIColor.orange)
        rBtn.setBackgroundImage(#imageLiteral(resourceName: "common_button_white"), for: .normal)
        rBtn.addTarget(self, action: #selector(registerBtnClick), for: .touchUpInside)
        return rBtn
        
    }()
    
    func registerBtnClick() {
        delegate?.registerClick()
    }
    
    

}












































